<?php
class Category_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->helper('date');
    }

    function getAll() {
        $query = $this->db->get('pr_category');
        return $query->result_array();
    }

    function insert() {
        $year = $this->input->post('year');
        $month = $this->input->post('month');
        $day = $this->input->post('day');
        $hour = $this->input->post('hour');
        $minute = $this->input->post('minute');
        $second = $this->input->post('second');

        $this->category = $this->input->post('name');
        $deadline = $year.'-'.$month.'-'.$day.' '.$hour.':'.$minute.':'.$second;
        $this->deadline = human_to_unix($deadline);
        if (days_in_month($month, $year) < $day || $hour > 23 || $minute > 59 || $second > 59)
            return FALSE;
        else {
            $this->db->insert('pr_category', $this);
            return TRUE;
        }
    }

    function select($id) {
        $query = $this->db->get_where('pr_category',array('id' => $id));
        return $query->row_array();
    }

    function edit() {
        $year = $this->input->post('year');
        $month = $this->input->post('month');
        $day = $this->input->post('day');
        $hour = $this->input->post('hour');
        $minute = $this->input->post('minute');
        $second = $this->input->post('second');
        $id = $this->input->get('id');

        $this->category = $this->input->post('name');
        $deadline = $year.'-'.$month.'-'.$day.' '.$hour.':'.$minute.':'.$second;
        $this->deadline = human_to_unix($deadline);
        if (days_in_month($month, $year) < $day || $hour > 23 || $minute > 59 || $second > 59)
            return FALSE;
        else {
            $this->db->update('pr_category', $this, array('id' => $id));
            return TRUE;
        }
    }

    function delete() {
        $id = $this->input->get('id');
        $this->db->delete('pr_category', array('id' => $id));
    }
}